#from CustomGuiItems.MyDockWidget import MyDockWidget;
from CustomGuiItems.MyTableWidget import MyTableWidget;
from PyQt4 import QtGui, QtCore;

class GridStTraceEvents(QtCore.QObject):    
    def __init__(self, objData):
        super(GridStTraceEvents, self).__init__();
        self.dataModel = objData.dataModel;
        self.objData = objData;
        self.parent = objData.parent;
        self.mainWin = objData.parent.mainWin;
#        self.myDockWidget = MyDockWidget("State Trace Events", self.parent);
#        self.mainWin.addDockWidget(QtCore.Qt.LeftDockWidgetArea, self.myDockWidget);
        self.createDockContent();
#        self.myDockWidget.setWidget(self.gridStTraceEvents);
        
    []#Update Grid Data from DataModel
    def updateGrid(self):
        stTraceEvents = self.dataModel.stTraceEvents;
        self.gridStTraceEvents.clearContents();
        self.gridStTraceEvents.setRowCount(self.dataModel.nTimeEvents);
        for i in range(len(stTraceEvents)):
            for j in range(3):
                qItem = QtGui.QTableWidgetItem(str(stTraceEvents[i][j]));
                qItem.setTextAlignment(QtCore.Qt.AlignHCenter | QtCore.Qt.AlignVCenter);
                self.gridStTraceEvents.setItem(i,j,qItem);
        self.gridStTraceEvents.resizeColumnsToContents();
        self.gridStTraceEvents.resizeRowsToContents();
        
    def onTableEdit(self, row, col):
        iData = int(self.gridStTraceEvents.item(row,col).data(0).toDouble()[0]);
        sData = self.gridStTraceEvents.item(row,col).data(0).toString();
        if sData!=str(iData): #Format the Input Number correctly
            self.gridStTraceEvents.setItem(row,col,QtGui.QTableWidgetItem(str(iData)));        
        self.dataModel.stTraceEvents[row][col] = iData;
        if self.parent.stTraceEvents!=None:
            self.parent.stTraceEvents.traceEvents.updateLbxTrace();
        
    def createDockContent(self):
        #Table
        self.gridStTraceEvents = MyTableWidget(self.mainWin);
        myItemDelegate = ItemDelegate_GralMdpParams(self.parent, self.gridStTraceEvents);
        self.gridStTraceEvents.setItemDelegate(myItemDelegate);
        self.gridStTraceEvents.setColumnCount(3);
        self.gridStTraceEvents.setRowCount(self.dataModel.nTimeEvents);
        self.gridStTraceEvents.setHorizontalHeaderLabels(["State", "Action", "Next State"]);
        self.gridStTraceEvents.resizeColumnsToContents();
        self.gridStTraceEvents.resizeRowsToContents();
        self.gridStTraceEvents.connect(self.gridStTraceEvents, QtCore.SIGNAL("cellChanged(int,int)"), self.onTableEdit);
        self.updateGrid();
                
    
class ItemDelegate_GralMdpParams(QtGui.QStyledItemDelegate):
    def __init__(self, parent=None, qTable=None):
        super(ItemDelegate_GralMdpParams, self).__init__(parent);
        self.qTable = qTable;
    
#    def paint(self, painter, option, index):
#        super(ItemDelegate_GralMdpParams, self).paint(painter, option, index);
#        painter.setRenderHint(QtGui.QPainter.Antialiasing);
#        nX = self.qTable.columnViewportPosition(index.column());
#        nY = self.qTable.rowViewportPosition(index.row());
#        nWidth = self.qTable.columnWidth(index.column());
#        nHeight = self.qTable.rowHeight(index.row());
#        if index.column()==1 and index.row()==3:
#            painter.drawEllipse(nX, nY, nWidth, nHeight);
#        elif index.column()==2 and index.row()==3:
#            myPen = QtGui.QPen(QtGui.QColor("Green"));
#            myPen.setWidth(6);
#            painter.setPen(myPen);
#            painter.drawRect(nX, nY, nWidth, nHeight);
